package com.base.myproject.client.busi.ccode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseBusiLeftPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

public class CcodeDetail extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	String ccode;
	BaseGrid basegrid;
	CcodeMain ccodemain;
	public CcodeDetail()
	{
		super();
		init();
	}
	public CcodeDetail(String ccode)
	{
		this();
		this.ccode = ccode;
		
	}
	
	
	
	public void init() {
		
		this.setWidth("1024px");
		//this.setHeight("720px");
		//this.setAutoHeight(true);
		
//		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
//		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.setAutoHeight(true);
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 5, 0, 0));
		this.add(initWidgetx(), centerData);
		
		
		
		registerMainStore(store);
		registerStore(store);
		
	}

	

	public ContentPanel CreateCcodeLeftPanel() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		columns.add(new ColumnConfig("cname", "供应商名称", 250));

		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("cname");
		type.getField("cname").setType(String.class);
		type.addField("httpurl");
		type.getField("httpurl").setType(String.class);
		type.addField("registercapital");
		type.getField("registercapital").setType(String.class);
		type.addField("employeenum");
		type.getField("employeenum").setType(String.class);
		type.addField("branchorgnum");
		type.getField("branchorgnum").setType(String.class);
		type.addField("artperson");
		type.getField("artperson").setType(String.class);
		type.addField("taxno");
		type.getField("taxno").setType(String.class);
		type.addField("partner");
		type.getField("partner").setType(String.class);
		type.addField("artno");
		type.getField("artno").setType(String.class);
		type.addField("createbcode");
		type.getField("createbcode").setType(String.class);
		type.addField("createdate");
		type.getField("createdate").setType(Date.class);
		type.addField("updatedate");
		type.getField("updatedate").setType(Date.class);
		type.addField("updatebcode");
		type.getField("updatebcode").setType(String.class);
		type.addField("ccodetype");
		type.getField("ccodetype").setType(Integer.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("ccode");
		info.setKeyname(new String[] { "icode" });
		info.addBusiParams("CODENAMEPARAMS", "{INC:6}");
		info.addBusiParams("CODENAMEFILTER", "1=1");
		info.addBusiParams("CODENAME", "ccode");
		info
				.setSql("select icode,ccode,cname,httpurl,registercapital,employeenum,branchorgnum,artperson,taxno,partner,artno,createbcode,createdate,updatedate,updatebcode,ccodetype from ccode ");
		
		BaseBusiLeftPanel<BusiObject> basbusileftpanel = new BaseBusiLeftPanel<BusiObject>(columns,
				type, info)
		{
			protected boolean Select(Store<BusiObject> store,
					BusiObject parent, BusiObject record, String property,
					String filter) {
				for (int i = 0; i < type.getFieldCount(); i++) {
					
					Object name = record.get(type.getField(i).getName());
				//状态用了码名映射，所以要对名字进行搜索
					if(type.getField(i).getName().equals("busistatus"))
					{
						if(name!=null && SystemMessage.getstatus(name+"").indexOf(filter.toLowerCase()) !=-1)
						{
							return true;
						}
						
					}
					
					else if (name!=null&&name.toString().indexOf(filter.toLowerCase()) != -1) {
						return true;
					}
				}
				return false;
			}
			
			public boolean candeleterecord()
			{
				if(this.getStore().getCount()<=0)
				{
					return false;
				}
				
				return true;
			}
			
			/*public void doBeforeRowChangeGrid()
			{
				if(store.isIschanged()  )
				{
					store.trySave();
				}
				
			}*/
			
			public void setdefault()
			{
				DateTimeFormat activeFormat = DateTimeFormat
				.getFormat("yyyy-M-d H:mm:ss");
				
				store.addDefault("createdate",activeFormat.format(new Date()));
				store.addDefault("ccodetype",2);
			}
		};
		store = basbusileftpanel.getStore();
		
	
		basbusileftpanel.getGrid().setAutoExpandColumn("cname");
		basbusileftpanel.getGrid().setWidth("320");
		basegrid = basbusileftpanel.getGrid();
		setMaincomponent(basegrid);
		return basbusileftpanel.getPanel();
	}
	private Widget initWidgetx() 
	{
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.setLayout(new BorderLayout());
		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 350);
		westData.setSplit(true);
		westData.setCollapsible(true);
		westData.setMargins(new Margins(2));
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(2));
		panel.add(CreateCcodeLeftPanel(), westData);
		VerticalPanel vpanel = new VerticalPanel();
	
		
		ccodemain = new CcodeMain(store);
		vpanel.add(ccodemain);
		CcodeLinkMan ccodelinkman =new CcodeLinkMan(store);
		vpanel.add( ccodelinkman);
		registerStore(ccodelinkman.getStore());
		panel.add(vpanel, centerData);
		//panel.setAutoWidth(true);
		panel.setHeight("450");
	
	return panel;
	}
	
//	private void initWidget() {
//		
//		//this.setHeaderVisible(false);
//		this.setLayout(new BorderLayout());
//		BorderLayoutData westData = new BorderLayoutData(LayoutRegion.WEST, 350);
//		westData.setSplit(true);
//		westData.setCollapsible(true);
//		westData.setMargins(new Margins(10));
//		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
//		centerData.setMargins(new Margins(10));
//		
//		this.add(CreateCcodeLeftPanel(), westData);
//		//CreateCcodeLeftPanel();
//		VerticalPanel vpanel = new VerticalPanel();
//	
//		
//		ccodemain = new CcodeMain(store);
//		vpanel.add(ccodemain);
//		CcodeLinkMan ccodelinkman =new CcodeLinkMan(store);
//		vpanel.add( ccodelinkman);
//		registerStore(ccodelinkman.getStore());
//		this.add(vpanel, centerData);
//		//panel.setAutoWidth(true);
//		this.setHeight("480");
//	}
	public String getCcode() {
		return ccode;
	}
	public void setCcode(String ccode) {
		this.ccode = ccode;
	}
	
	public void save()
	{
		if(ccodemain.isValid())
		{
			super.save();
		}
		else
		{
			Window.alert("有必录项，没有录入");
		}
	}

	
}
